﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

/// <summary>
///Cacheing 的摘要说明
/// </summary>

public class Cacheing
{

    static public object Get(string CacheId)
    {
        object objCache = System.Web.HttpRuntime.Cache.Get(CacheId);
        return objCache;
    }
    /// <summary>
    /// 寫入 Cache 資料
    /// </summary>
    /// <param name="CacheId"></param>
    /// <param name="objCache"></param>
    /// <param name="cacheDurationSeconds"></param>
    static public void Add(string CacheId, object objCache, int cacheDurationSeconds)
    {
        if (objCache != null)
        {
            System.Web.HttpRuntime.Cache.Insert(
                CacheId,
                objCache,
                null,
                System.Web.Caching.Cache.NoAbsoluteExpiration,
                new TimeSpan(0, 0, cacheDurationSeconds),
                System.Web.Caching.CacheItemPriority.High,
                null);
        }
    }
    public static void Insert(string key, string value)
    {
        System.Web.HttpRuntime.Cache.Insert(key, value);
    }
    public static void Set(string key, string value)
    {
        System.Web.HttpRuntime.Cache.Insert(key, value);
    }
    /// <summary>
    /// Insert value into the cache using
    /// appropriate name/value pairs
    /// </summary>
    /// <typeparam name="T">Type of cached item</typeparam>
    /// <param name="o">Item to be cached</param>
    /// <param name="key">Name of item</param>
    public static void Add<T>(T objCache, string key) where T : class
    {
        // NOTE: Apply expiration parameters as you see fit.
        // In this example, I want an absolute
        // timeout so changes will always be reflected
        // at that time. Hence, the NoSlidingExpiration.
        if (objCache != null)
        {
            System.Web.HttpRuntime.Cache.Insert(key, objCache, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 60, 0), System.Web.Caching.CacheItemPriority.High, null);
        }
    }

    /// <summary>
    /// Remove item from cache
    /// </summary>
    /// <param name="key">Name of cached item</param>
    public static void Clear(string key)
    {
        System.Web.HttpRuntime.Cache.Remove(key);
    }
    /// <summary>
    /// Remove item from cache
    /// </summary>
    /// <param name="key">Name of cached item</param>
    public static void LikeClear(string key)
    {
        IDictionaryEnumerator enumerator = System.Web.HttpRuntime.Cache.GetEnumerator();
        List<string> list = new List<string>();
        while (enumerator.MoveNext())
        {
            list.Add(enumerator.Key.ToString());
        }

        Regex reg = new Regex(key);
        foreach (string cKey in list)
        {
            if (reg.IsMatch(cKey))
            {
                HttpContext.Current.Cache.Remove(cKey);
            }
        }

    }

    /// <summary>
    /// Check for item in cache
    /// </summary>
    /// <param name="key">Name of cached item</param>
    /// <returns></returns>
    public static bool Exists(string key)
    {
        return System.Web.HttpRuntime.Cache.Get(key) != null;
    }

    /// <summary>
    /// Retrieve cached item
    /// </summary>
    /// <typeparam name="T">Type of cached item</typeparam>
    /// <param name="key">Name of cached item</param>
    /// <returns>Cached item as type</returns>
    public static T Get<T>(string key) where T : class
    {
        try
        {
            return (T)System.Web.HttpRuntime.Cache.Get(key);
        }
        catch
        {
            return null;
        }
    }

    public static List<T> GetList<T>(string key) where T : class
    {
        try
        {
            return (List<T>)System.Web.HttpRuntime.Cache.Get(key);
        }
        catch
        {
            return null;
        }
    }
}
